package com.yz.kafka.consumers.wakeup;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.errors.WakeupException;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author yuanzheng
 * @since 2021/3/7
 */
public class SimpleConsumerWakeup {
    public static void main(String[] args) {
        String topic = "Hello-Kafka";
        String group = "group1";
        Properties props = new Properties();
        props.put("bootstrap.servers", "hadoop001:9092");
        /*指定分组 ID*/
        props.put("group.id", group);
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        /*订阅主题 (s)*/
        consumer.subscribe(Collections.singletonList(topic));
        /*调用 wakeup 优雅的退出*/
        final Thread mainThread = Thread.currentThread();
        new Thread(() -> {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                if ("exit".equals(sc.next())) {
                    consumer.wakeup();
                    try {
                        /*等待主线程完成提交偏移量、关闭消费者等操作*/
                        mainThread.join();
                        break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.of(100,
                        ChronoUnit.MILLIS));
                for (ConsumerRecord<String, String> rd : records) {
                    System.out.printf("topic = %s,partition = %d, key = %s, value = %s, offset = % d, ", rd.topic(), rd.partition(), rd.key(), rd.value(), rd.offset());
                }
            }
        } catch (WakeupException e) {
            //对于 wakeup() 调用引起的 WakeupException 异常可以不必处理
        } finally {
            consumer.close();
            System.out.println("consumer 关闭");
        }
    }
}
